#include "SsiPool.h"
#include <algorithm>

namespace Impp
{

SsiPool::SsiPool() :
	items(NULL)
{
}

std::vector<Item>* SsiPool::getSsiContainer(){
	if(this->items == NULL){
		this->items = new std::vector<Item>();
	}
	return this->items;
}

void SsiPool::reset(){
	if(items != NULL){
		for (size_t i = 0; i < items->size(); ++i) {
				Item &item = (*items)[i];
				for (size_t j = 0; j < item.data.size(); ++j) {
					deleteAllocedTlv(&item.data[j]);
				}
		}
		
		delete items;
		items = NULL;
	}
}

Item* SsiPool::getItem(Buddy* buddy){
	if(items == NULL){
		return NULL;
	}
	for (size_t i = 0; i < items->size(); ++i) {
			Item* item = &((*items)[i]);
			if(item->typeOfItems == SSI_BUDDY_RECORD && item->itemName == buddy->getScreenName()){
				return item;
			}
	}
	return NULL;
}

Item* SsiPool::getItem(Group* group){
	if(items == NULL){
		return NULL;
	}
	for (size_t i = 0; i < items->size(); ++i) {
			Item* item = &((*items)[i]);
			//TODO: not use name to check item!
			if(item->typeOfItems == SSI_GROUP_RECORD && item->itemName == group->getName()){
				return item;
			}
	}
	return NULL;
}

Item* SsiPool::getItem(const SsiType & type){
	if(items == NULL){
		return NULL;
	}
	for (size_t i = 0; i < items->size(); ++i) {
			Item* item = &((*items)[i]);
			if(item->typeOfItems == type){
				return item;
			}
	}
	return NULL;
}

Item* SsiPool::getItem(const SsiType & type,const wxString & itemName){
	if(items == NULL){
		return NULL;
	}
	for (size_t i = 0; i < items->size(); ++i) {
			Item* item = &((*items)[i]);
			if(item->typeOfItems == type && item->itemName == itemName){
				return item;
			}
	}
	return NULL;
}

bool SsiPool::deleteItem(Item* itemToDelete){
	if(items!=NULL){
		std::vector<Item>::iterator i(items->begin()); 
		for (; i != items->end(); ++i) { 
			if(&(*i) == itemToDelete){
				for (size_t j = 0; j < itemToDelete->data.size(); ++j) {
					deleteAllocedTlv(&itemToDelete->data[j]);
				}
				items->erase(i);
				return true;
			}
		}
	}
	return false;
}

int SsiPool::getPrivacySetting(){
	Item* item = getItem(SSI_PERMIT_DENY_SETTINGS);
	if(item!=NULL){
		std::vector<Tlv>::iterator i(item->data.begin()); 
		for (; i != item->data.end(); ++i) { 
		      if((*i).type==0x00CA){ //privacy settings
		    	  return (int) *(*i).data;
		      }
		}
	}
	return -1;
}

word SsiPool::getNewItemId(){
	if(items == NULL){
		return 0;
	}
	//List with all ids in SsiPool
	std::vector<word> ids;
	
	//push all ids in list
	std::vector<Item>::iterator i(items->begin()); 
	for (; i != items->end(); ++i) { 
	     ids.push_back((*i).itemId);
	    // wxLogDebug(wxT("itemName %s ItemId: %X"),(*i).itemName.c_str(),(*i).itemId);
	}
	
	//sort list to find the lowst free id
	std::sort(ids.begin(),ids.end());
	
	word ret = 0;
	
	//find the lowest free id:
	std::vector<word>::iterator i3(ids.begin()); 
	for (; i3 != ids.end(); ++i3) { 
		std::vector<word>::iterator next = i3+1;
		if(next != ids.end()){
			if(*i3+1 != *next && *i3 != *next){
				ret = *i3+1;
				break;
			}
		}else{
			ret = *i3+1;
		}
	}
	
	if(ret==0){
		ret = 1;
	}
	
	wxLogDebug(wxT("new Id is: %X"),ret);
	
	return ret;
}

SsiPool::~SsiPool()
{
	reset();
}

}

